//
//  XLFirimAppDisTool.m
//  XLFirimAppDis
//
//  Created by ilongge on 2022/5/20.
//

#import "XLFirimAppDisTool.h"
#import "ProjectNetworkManager.h"
#import "XLFirimAppDisLastestInfo.h"

#define WeakSelf(weakSelf)              __weak __typeof(&*self) weakSelf = self;
#define StrongSelf(strongSelf)          __strong __typeof(&*self) strongSelf = weakSelf;

@interface XLFirimAppDisTool()
@property (nonatomic, copy) NSString *appId;
@property (nonatomic, copy) NSString *api_token;
@end
@implementation XLFirimAppDisTool
static XLFirimAppDisTool *_tool = nil;
static dispatch_once_t _onceToken = 0;
#pragma mark ---------------------------------- 初始化 类
+ (instancetype)shareManager
{
    if (_tool == nil) {
        _tool = [[super alloc]init];
    }
    return _tool;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    dispatch_once(&_onceToken, ^{
        if (_tool == nil) {
            _tool = [super allocWithZone:zone];
        }
    });
    return _tool;
}

- (id)copyWithZone:(nullable NSZone *)zone {
    return _tool;
}

- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return _tool;
}

+ (void)destoryManager
{
    _tool.appId = nil;
    _tool.api_token = nil;
    _tool = nil;
    _onceToken = 0;
}

- (void)configAppDisToolWithAppID:(NSString *)appId andApiToken:(NSString *)api_token
{
    XLFirimAppDisTool *tool = [XLFirimAppDisTool shareManager];
    tool.appId = appId;
    tool.api_token = api_token;
}

+ (BOOL)checkConfig
{
    XLFirimAppDisTool *tool = [XLFirimAppDisTool shareManager];
    return [tool checkConfig];
}

+(void)checkAppUpdate
{
    XLFirimAppDisTool *tool = [XLFirimAppDisTool shareManager];
    [tool checkAppUpdate];
}

- (void)checkAppUpdate
{
    if ([self checkConfig]) {
        NSString *latestUri = [NSString stringWithFormat:@"%@%@%@",BaseUrl, XLAppLatestURI, self.appId];
        WeakSelf(weakSelf);
        [ProjectNetworkManager netWorkRequestWithType:RequestSerializerTypeGet
                                               andUrl:latestUri
                                         andParamters:@{@"api_token": self.api_token}
                                   andResponseHandler:^(NSDictionary *data, BOOL isSuccess, NSString *message) {
            StrongSelf(strongSelf);
            if (isSuccess) {
                XLFirimAppDisLastestInfo *latestAppInfo = [[XLFirimAppDisLastestInfo alloc] initWith:data];
                if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(xl_firimAppDisLatest:)]) {
                    Class class = [[[UIApplication sharedApplication] delegate] class];
                    NSDictionary *infoDict = [[NSBundle bundleForClass:class] infoDictionary];
                    NSString *versionShort = [infoDict objectForKey:@"CFBundleShortVersionString"];
                    NSString *buildVersion = [infoDict objectForKey:@"CFBundleVersion"];
                    if ([versionShort compare:latestAppInfo.versionShort] == NSOrderedAscending) {
                        latestAppInfo.needUpdate = YES;
                    }
                    else if ([buildVersion compare:latestAppInfo.version] == NSOrderedAscending) {
                        latestAppInfo.needUpdate = YES;
                    }
                    [strongSelf.delegate xl_firimAppDisLatest:latestAppInfo];
                }
            }
            else{
                if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(xl_firimAppDisErrorMsg:)]) {
                    [strongSelf.delegate xl_firimAppDisErrorMsg:@"请求信息失败"];
                }
            }
        }];
    }
    else{
        if (self.delegate && [self.delegate respondsToSelector:@selector(xl_firimAppDisErrorMsg:)]) {
            [self.delegate xl_firimAppDisErrorMsg:@"请检查appid或api_token"];
        }
    }
}
- (BOOL)checkConfig
{
    if (self.appId.length != 24) {
        return NO;
    }
    
    if (self.api_token.length != 32) {
        return NO;
    }
    
    return YES;
}


@end
